﻿// -----------------------------------------------------------------------
// <copyright file="sMenu.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace MenuGame2DXNA
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework;
    using System.Xml;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Media;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class sMenu : Scene
    {
       
        private Game game;
        private KeyboardState oldState;
        private List<MyXNAButton> Options;
        private List<Vector2> TempTopLeft;
        private int iSelected;
        private int  tick ;
        int nOption;
        public override void LoadContent(Game _game)
        {
            tick = 0;
            game = _game;
         
            // LoadMenu From Xml
            Stop();

            base.LoadContent(game);
        }

        private void InitMenu(string name)
        {
            nOption = 0;
            XmlDocument xdom = new XmlDocument();
            xdom.Load(name);

            XmlElement bg = (XmlElement)xdom.SelectSingleNode("Form/textures/texture");

           


            Options = new List<MyXNAButton>();
            XmlElement Buttons = (XmlElement)xdom.SelectSingleNode("Form/buttons");

            foreach (XmlElement button in Buttons.ChildNodes)
            {
                MyXNAButton tempButton = new MyXNAButton(game.Content, button.OuterXml, "Menu/");
                //tempButton.
                Options.Add(tempButton);
                nOption++;
            }
            iSelected = 0;
            TempTopLeft = new List<Vector2>();
            for (int i = 0; i < Options.Count; i++)
            {
                TempTopLeft.Add(Options[i].TopLeft);
                Options[i].TopLeft = new Vector2(-500, -500);
            }
        }
        static Random rr = new Random();
        EffectStype ee = (EffectStype) rr.Next(3);
        public override void Update(GameTime gameTime)
        {
            if (!isPlayMusic)
            {
                MusicManager.Play("Title");
                isPlayMusic = true;
            }
            tick ++;
            KeyboardState kbState = Keyboard.GetState();
            if (kbState.IsKeyDown(Keys.Enter))
            {
                if (!oldState.IsKeyDown(Keys.Enter))
                {
                    if (nOption == 6)
                    {
                        CommandSelected();
                    }
                    else
                        CommandSelected2();
                    
                }

            }

            if (kbState.IsKeyDown(Keys.Down))
            {
                if (!oldState.IsKeyDown(Keys.Down))
                {
                    SoundManager.Play("click");
                    if (iSelected < nOption-1)
                        iSelected++;
                    else
                        iSelected = 0;

                }

            }

            // kbState.
            if (kbState.IsKeyDown(Keys.Up))
            {

                if (!oldState.IsKeyDown(Keys.Up))
                {
                    SoundManager.Play("click");
                    if (iSelected > 0)
                        iSelected--;
                    else
                        iSelected = nOption-1;
                }

            }

            oldState = kbState;
            

            int speeed = 5;
           
            for (int i = 0; i < Options.Count; i++)
            {
                Effect(i, gameTime, i * speeed, (i + 2) * speeed,ee);
            }
            for (int i = 0; i < Options.Count; i++)
            {
                Options[i].SetSprite(0);
                Options[i].Update(gameTime, Color.White);

            }
            if (iSelected >= 0)
                Options[iSelected].SetSprite(1);
        }
        private void CommandSelected()
        {
            Pause();
            switch (iSelected)
            {
                case 0:
                    game.Scene = 6;
                    break;
                case 1:
                    game.Scene = 2;
                    break;
                case 2:
                    game.Scene = 5;
                    break;
                case 3:
                    game.Scene = 8;
                    break;
                case 4:
                    game.Scene = 3;
                    break;
               
                case 5:
                    game.Exit();
                    break;
            }
        }
        private void CommandSelected2()
        {
            Pause();
            switch (iSelected)
            {
                case 0:
                    game.Scene = 1;
                    break;
                case 1:
                    game.Scene = 6;
                    game.ListScene[1].Stop();
                    break;
                case 2:
                    game.Scene = 2;
                    break;
                case 3:
                    game.Scene = 5;
                    break;
                case 4:
                    game.Scene = 8;
                    break;
                case 5:
                    game.Scene = 3;
                    break;
                case 6:              
                    game.Exit();
                    break;
            }
        }
        private bool GetSelectedIndex(Vector2 point)
        {
            for (int i = 0; i < Options.Count; i++)
            {
                if (Options[i].IsClicked(point))
                {
                    iSelected = i;

                    return true;
                }
            }
            return false;
        }
       
        protected void Effect(int IndexOptions, GameTime gameTime, int TimeStart, int TimeFinish, EffectStype Stype)
        {
            
            if (tick >= TimeStart && tick <= TimeFinish)
            {
                int i = tick;
                switch (Stype)
                {
                    case EffectStype.TopDown:
                        {

                            if (i * 20 < TempTopLeft[IndexOptions].Y)
                                Options[IndexOptions].TopLeft = new Vector2(TempTopLeft[IndexOptions].X, i * 20);
                            else
                            {
                                Options[IndexOptions].TopLeft = TempTopLeft[IndexOptions];
                            }
                            break;
                        }
                    case EffectStype.LeftRight:
                        {
                            if (i * 20 < TempTopLeft[IndexOptions].X)
                                Options[IndexOptions].TopLeft = new Vector2(i * 20, TempTopLeft[IndexOptions].Y);
                            else
                            {
                                Options[IndexOptions].TopLeft = TempTopLeft[IndexOptions];
                            }
                            break;
                        }
                    case EffectStype.oblique:
                        {
                            if (i * 20 < TempTopLeft[IndexOptions].X && i * 20 < TempTopLeft[IndexOptions].Y)
                                Options[IndexOptions].TopLeft = new Vector2(i * 20, i * 20);
                            else
                            {
                                Options[IndexOptions].TopLeft = TempTopLeft[IndexOptions];
                            }
                            break;
                        }
                }
            }

        }
        public enum EffectStype { TopDown = 0, LeftRight, oblique };

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);


            for (int i = 0; i < Options.Count; i++)
                Options[i].Draw(gameTime, spriteBatch);
        }
        public override void Play()
        {
            InitMenu("Menu/Menu2.xml");
            base.Play();
        }
        public override void Stop()
        {
            base.Stop();
        }
        public override void Pause()
        {       
            base.Pause();
        }
        public override void Resume()
        {
            tick = 0;
            if (game.ListScene[1].State == SceneState.STOP)
                InitMenu("Menu/Menu2.xml");
            else
                InitMenu("Menu/Menu.xml");
            base.Resume();
        }
    }
}
